﻿' ----------------------------------------------------------------------------------
' Microsoft Developer & Platform Evangelism
' 
' Copyright (c) Microsoft Corporation. All rights reserved.
' 
' THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
' EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
' OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ----------------------------------------------------------------------------------
' The example companies, organizations, products, domain names,
' e-mail addresses, logos, people, places, and events depicted
' herein are fictitious.  No association with any real company,
' organization, product, domain name, email address, logo, person,
' places, or events is intended or should be inferred.
' ----------------------------------------------------------------------------------

Imports Microsoft.Samples.WindowsPhoneCloud.StorageClient.Credentials
Imports System.Windows.Threading
Imports System.Globalization

Namespace ViewModel

    Public Class RegisterPageViewModel
        Inherits BaseViewModel
#If ACS Then
        Private ReadOnly authenticationClient As IRegistrationClient
        Private ReadOnly dispatcher As Dispatcher

        Private _sslCertificateUri As Uri
        Private _userName As String
        Private _email As String
        Private _isBusy As Boolean

        Public Sub New()
            Me.New(App.CloudClientFactory.ResolveRegistrationClient(), TryCast(Application.Current.Resources("SSLCertificateUrl"), String), Deployment.Current.Dispatcher)
        End Sub

        Public Sub New(ByVal authenticationClient As IRegistrationClient, ByVal sslCertificateUrl As String, ByVal dispatcher As Dispatcher)
            Me.authenticationClient = authenticationClient
            Me.dispatcher = dispatcher

            Dim uri As Uri = Nothing
            Uri.TryCreate(sslCertificateUrl, UriKind.Absolute, uri)
            Me._sslCertificateUri = uri
        End Sub

        Public ReadOnly Property IsSslCertificateLinkVisible() As Boolean
            Get
                Return Me.SslCertificateUri IsNot Nothing
            End Get
        End Property

        Public Property SslCertificateUri() As Uri
            Get
                Return Me._sslCertificateUri
            End Get

            Set(ByVal value As Uri)
                If Me._sslCertificateUri <> value Then
                    Me._sslCertificateUri = value
                    Me.NotifyPropertyChanged("SslCertificateUri")
                    Me.NotifyPropertyChanged("IsSslCertificateLinkVisisble")
                End If
            End Set
        End Property

        Public Property UserName() As String
            Get
                Return Me._userName
            End Get

            Set(ByVal value As String)
                If Me._userName <> value Then
                    Me._userName = value
                    Me.NotifyPropertyChanged("UserName")
                    Me.NotifyPropertyChanged("IsRegisterEnabled")
                End If
            End Set
        End Property

        Public Property EMail() As String
            Get
                Return Me._email
            End Get

            Set(ByVal value As String)
                If Me._email <> value Then
                    Me._email = value
                    Me.NotifyPropertyChanged("EMail")
                    Me.NotifyPropertyChanged("IsRegisterEnabled")
                End If
            End Set
        End Property

        Public Property IsBusy() As Boolean
            Get
                Return Me._isBusy
            End Get

            Set(ByVal value As Boolean)
                If Me._isBusy <> value Then
                    Me._isBusy = value
                    Me.NotifyPropertyChanged("IsBusy")
                End If
            End Set
        End Property

        Public ReadOnly Property IsRegisterEnabled() As Boolean
            Get
                Return (Not String.IsNullOrEmpty(Me.UserName)) AndAlso Not String.IsNullOrEmpty(Me.email)
            End Get
        End Property

        Public Sub Register(ByVal successCallback As Action, ByVal exceptionCallback As Action(Of String))
            Me.IsBusy = True
            Try
                Me.authenticationClient.Register(
                    Me.UserName,
                    Me.EMail,
                    Sub(args)
                        Me.dispatcher.BeginInvoke(
                            Sub()
                                successCallback()
                                Me.IsBusy = False
                            End Sub)
                    End Sub,
                    Sub(args)
                        Me.dispatcher.BeginInvoke(
                            Sub()
                                Me.IsBusy = False
                                Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error in registration. Please try again later." & vbLf & "{0} {1}", args.Exception.Message, If(args.Exception.StatusCode = HttpStatusCode.NotFound, "Please, make sure to install the SSL certificate in the phone.", String.Empty))
                                exceptionCallback(errorMessage)
                            End Sub)
                    End Sub)
            Catch ex As Exception
                Me.IsBusy = False
                exceptionCallback(ex.Message)
            End Try
        End Sub
#Else
        Private ReadOnly authenticationClient As IAuthenticationClient
        Private ReadOnly dispatcher As Dispatcher

        Private _userName As String
        Private _password As String
        Private _confirmPassword As String
        Private _Renamed As String
        Private _isBusy As Boolean

        Public Sub New()
            Me.New(App.CloudClientFactory.ResolveAuthenticationClient(), Deployment.Current.Dispatcher)
        End Sub

        Public Sub New(ByVal authenticationClient As IAuthenticationClient, ByVal dispatcher As Dispatcher)
            Me.authenticationClient = authenticationClient
            Me.dispatcher = dispatcher
        End Sub

        Public Property UserName() As String
            Get
                Return Me._userName
            End Get

            Set(ByVal value As String)
                If Me._userName <> value Then
                    Me._userName = value
                    Me.NotifyPropertyChanged("UserName")
                    Me.NotifyPropertyChanged("IsRegisterEnabled")
                End If
            End Set
        End Property

        Public Property Password() As String
            Get
                Return Me._password
            End Get

            Set(ByVal value As String)
                If Me._password <> value Then
                    Me._password = value
                    Me.NotifyPropertyChanged("Password")
                    Me.NotifyPropertyChanged("IsRegisterEnabled")
                End If
            End Set
        End Property

        Public Property EMail() As String
            Get
                Return Me._Renamed
            End Get

            Set(ByVal value As String)
                If Me._Renamed <> value Then
                    Me._Renamed = value
                    Me.NotifyPropertyChanged("EMail")
                    Me.NotifyPropertyChanged("IsRegisterEnabled")
                End If
            End Set
        End Property

        Public Property ConfirmPassword() As String
            Get
                Return Me._confirmPassword
            End Get

            Set(ByVal value As String)
                If Me._confirmPassword <> value Then
                    Me._confirmPassword = value
                    Me.NotifyPropertyChanged("ConfirmPassword")
                    Me.NotifyPropertyChanged("IsRegisterEnabled")
                End If
            End Set
        End Property

        Public Property IsBusy() As Boolean
            Get
                Return Me._isBusy
            End Get

            Set(ByVal value As Boolean)
                If Me._isBusy <> value Then
                    Me._isBusy = value
                    Me.NotifyPropertyChanged("IsBusy")
                End If
            End Set
        End Property

        Public ReadOnly Property IsRegisterEnabled() As Boolean
            Get
                Return (Not String.IsNullOrEmpty(Me.UserName)) AndAlso (Not String.IsNullOrEmpty(Me.EMail)) AndAlso (Not String.IsNullOrEmpty(Me.Password)) AndAlso Not String.IsNullOrEmpty(Me.ConfirmPassword)
            End Get
        End Property

        Public Sub Register(ByVal userRegisteredCallback As Action, ByVal userNotRegisteredCallback As Action(Of String))
            Me.IsBusy = True
            Me.authenticationClient.Register(
                Me.UserName,
                Me.EMail,
                Me.Password,
                Me.ConfirmPassword,
                Sub(args)
                    Me.dispatcher.BeginInvoke(
                        Sub()
                            Me.IsBusy = False
                            userRegisteredCallback()
                        End Sub)
                End Sub,
                Sub(args)
                    Me.dispatcher.BeginInvoke(
                        Sub()
                            Me.IsBusy = False
                            Dim errorMessage = String.Format(CultureInfo.InvariantCulture, "Error in registration. Please try again later." & vbLf & "{0} {1}", args.Exception.Message, If(args.Exception.StatusCode = HttpStatusCode.NotFound, "Please, make sure to install the SSL certificate in the phone.", String.Empty))
                            userNotRegisteredCallback(errorMessage)
                        End Sub)
                End Sub)
        End Sub
#End If

    End Class
End Namespace